package org.libra {
	import flash.events.Event;
	import flash.text.TextFormatAlign;
	import org.libra.basic.Container;
	import org.libra.events.LibraEvent;
	import org.libra.events.PageEvent;
	import org.libra.geom.IntDimension;
	import org.libra.geom.IntPoint;
	import org.libra.ui.interfaces.IButtonState;
	import org.libra.utils.JColor;
	import org.libra.utils.JFont;
	
	/**
	 * ...
	 * @author zhangzuoqiang
	 */
	public class JPageCounter extends Container {
		
		private var frontBtn:JButton;
		private var backBtn:JButton;
		private var prevBtn:JButton;
		private var nextBtn:JButton;
		private var loop:Boolean;
		private var counter:JLabel;
		private var minNum:int;
		private var maxNum:int;
		private var curNum:int;
		private var extreme:Boolean;
		
		public function JPageCounter(extremeBtnSize:IntDimension, btnSize:IntDimension, counterSize:IntDimension) { 
			super();
			extreme = extremeBtnSize ? true : false;
			if (extreme) {
				frontBtn = new JButton();
				frontBtn.setSize(extremeBtnSize);
				backBtn = new JButton();
				backBtn.setSize(extremeBtnSize);
			}
			prevBtn = new JButton();
			prevBtn.setSize(btnSize);
			nextBtn = new JButton();
			nextBtn.setSize(btnSize);
			minNum = 1;
			maxNum = 100;
			curNum = 1;
			loop = false;
			counter = new JLabel();
			counter.setSize(counterSize);
			counter.setAlign(TextFormatAlign.CENTER);
			this.setCoutnerText(curNum, maxNum);
			this.appendAll(prevBtn, nextBtn, counter);
			if (extreme) {
				this.appendAll(frontBtn, backBtn);
			}
		}
		
		public function setFrontBtnState(state:IButtonState):void {
			if (extreme) {
				this.frontBtn.setState(state);
			}
		}
		
		public function setBackBtnState(state:IButtonState):void {
			if (extreme) {
				this.backBtn.setState(state);
			}
		}
		
		public function setPrevBtnState(state:IButtonState):void {
			this.prevBtn.setState(state);
		}
		
		public function setNextBtnState(state:IButtonState):void {
			this.nextBtn.setState(state);
		}
		
		public function setFrontBtnLocation(l:IntPoint):void {
			if (extreme) {
				this.frontBtn.setLocation(l);
			}
		}
		
		public function setPrevBtnLocation(l:IntPoint):void {
			this.prevBtn.setLocation(l);
		}
		
		public function setNextBtnLocation(l:IntPoint):void {
			this.nextBtn.setLocation(l);
		}
		
		public function setBackBtnLocation(l:IntPoint):void {
			if (extreme) {
				this.backBtn.setLocation(l);
			}
		}
		
		public function setCounterLocation(l:IntPoint):void {
			this.counter.setLocation(l);
		}
		
		public function setFont(font:JFont):void {
			this.counter.setFont(font);
		}
		
		override public function setForeground(c:JColor):void {
			super.setForeground(c);
			this.counter.setForeground(c);
		}
		
		public function isLoop():Boolean {
			return this.loop;
		}
		
		public function setLoop(value:Boolean):void {
			this.loop = value;
		}
		
		public function setMinNum(value:int):void {
			if (minNum != value) {
				if (value <= maxNum) {
					this.minNum = value;
					curNum = curNum > minNum ? curNum : minNum;
					setCoutnerText(curNum, maxNum);
				}
			}
		}
		
		public function getMinNum():int {
			return minNum;
		}
		
		public function setMaxNum(value:int):void {
			if (maxNum != value) {
				if (value >= minNum) {
					this.maxNum = value;
					this.curNum = this.curNum < this.maxNum ? this.curNum : maxNum;
					setCoutnerText(curNum, maxNum);
				}
			}
		}
		
		public function getMaxNum():int {
			return maxNum;
		}
		
		public function getCurNum():int {
			return this.curNum;
		}
		
		public function setCurNum(value:int):void {
			if (this.curNum != value) {
				if (value >= minNum && value <= maxNum) {
					this.curNum = value;
					setCoutnerText(curNum, maxNum);
					dispatchEvent(new PageEvent(PageEvent.PAGE_CHANGED, minNum, maxNum, curNum));
				}
			}
		}
		
		private function setCoutnerText(curNum:int, maxNum:int):void {
			this.counter.setText(curNum + "/" + maxNum);
		}
		
		override public function setOpaque(b:Boolean):void {
			super.setOpaque(b);
			this.counter.setOpaque(b);
			this.prevBtn.setOpaque(b);
			this.nextBtn.setOpaque(b);
			if (extreme) {
				this.frontBtn.setOpaque(b);
				this.backBtn.setOpaque(b);
			}
		}
		
		override protected function onAddToStageHandler(e:Event):void {
			super.onAddToStageHandler(e);
			this.prevBtn.addActionListener(onButtonHandler);
			this.nextBtn.addActionListener(onButtonHandler);
			if (extreme) {
				this.frontBtn.addActionListener(onButtonHandler);
				this.backBtn.addActionListener(onButtonHandler);
			}
		}
		
		override protected function onRemoveFromStageHandler(e:Event):void {
			super.onRemoveFromStageHandler(e);
			this.prevBtn.removeActionListener(onButtonHandler);
			this.nextBtn.removeActionListener(onButtonHandler);
			if (extreme) {
				this.frontBtn.removeActionListener(onButtonHandler);
				this.backBtn.removeActionListener(onButtonHandler);
			}
		}
		
		private function onButtonHandler(evt:LibraEvent):void {
			var changed:Boolean = false;
			switch(evt.target) {
				case prevBtn:
					if (this.curNum == minNum) {
						if (loop) {
							curNum = maxNum;
							changed = true;
						}
					}else {
						curNum--;
						changed = true;
					}
				break;
				case nextBtn:
					if (curNum == maxNum) {
						if (loop) {
							curNum = minNum;
							changed = true;
						}
					}else {
						curNum += 1;
						changed = true;
					}
				break;
				case frontBtn:
					if (curNum > minNum) {
						curNum = minNum;
						changed = true;
					}
				break;
				case backBtn:
					if (curNum < maxNum) {
						curNum = maxNum;
						changed = true;
					}
				break;
			}
			if (changed) {
				this.setCoutnerText(curNum, maxNum);
				dispatchEvent(new PageEvent(PageEvent.PAGE_CHANGED, minNum, maxNum, curNum));
			}
		}
		
		override public function toString():String {
			return "I am JPageCounter";
		}
		
	}

}